#include<stdio.h>
#include<unistd.h>
#include<pthread.h>

pthread_cond_t cond;
pthread_mutex_t lock;
void* r1(void* arg)
{
	while(1)
	{
	pthread_cond_wait(&cond,&lock);
	printf("%s:活动中...\n",(char*)arg);
	}
	return NULL;
}
void* r2(void* arg)
{
	while(1)
	{
		//printf("%s:随机唤醒一个线程...\n",(char*)arg);
		printf("%s:唤醒所有等待条件变量的线程...\n",(char*)arg);
		pthread_cond_broadcast(&cond);
		sleep(1);
	}
	return NULL;
}
int main()
{
	pthread_cond_init(&cond,NULL);
	pthread_mutex_init(&lock,NULL);

	pthread_t t1,t2,t3,t4;
	pthread_create(&t1,NULL,r1,"Thread 1");
	pthread_create(&t2,NULL,r1,"Thread 2");
	pthread_create(&t3,NULL,r1,"Thread 3");
	pthread_create(&t4,NULL,r2,"Thread 4");

	pthread_join(&t1,NULL);
	pthread_join(&t2,NULL);
	pthread_join(&t3,NULL);
	pthread_join(&t4,NULL);

	pthread_cond_destroy(&cond);
	pthread_mutex_destroy(&lock);
	return 0;
}
